import { Injectable } from '@nestjs/common'
import { CreateCommitDto } from './dto/create-commit.dto'
import { UpdateCommitDto } from './dto/update-commit.dto'
import { Commit } from './entities/commit.entity'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository, Like } from 'typeorm'
import { Post } from 'src/post/entities/post.entity'
@Injectable()
export class CommitService {
  constructor(
    @InjectRepository(Commit) private readonly commit: Repository<Commit>,
    @InjectRepository(Post) private readonly post: Repository<Post>
  ) {}
  create(createCommitDto: CreateCommitDto) {
    return 'This action adds a new commit'
  }

  findAll() {
    return `This action returns all commit`
  }

  findOne(id: string) {
    return this.commit.findOne({
      // relations: ['user'],
      where: {
        id: id,
      },
      order: {
        create_time: 'DESC', //倒序
        id: 'ASC', //正序
      },
    })
  }
 async findAllByPostId(id: string) {
   const res = await this.post.findOne({
      where: {
        id:id
      }
   })
  console.log(res);
  
   return this.commit.find({
      relations:['post'],
      where: {
        post: res[0],
      },
      order: {
        create_time: 'DESC', //倒序
        id: 'ASC', //正序
      },
    })
  }

  update(id: number, updateCommitDto: UpdateCommitDto) {
    return `This action updates a #${id} commit`
  }

  remove(id: number) {
    return `This action removes a #${id} commit`
  }
}
